package afinadroid.salesforce.test401;

import java.util.ArrayList;
import java.util.HashSet;

import afinadroid.magalindo8.salesforce401test.R;
import afinadroid.salesforce.bbdd.AccesoBD;
import afinadroid.salesforce.test401.models.Pregunta;
import afinadroid.salesforce.test401.utils.TestApplication;
import afinadroid.salesforce.test401.utils.Utils;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.LinearLayout;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.widget.TextView;

import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.AdSize;
import com.google.android.gms.ads.AdView;



public class MainActivity extends Activity {

	private SeekBar mSeekbar;
	private AccesoBD bd;

	// Variables publicidad
	private AdView adView;
	private static String UNIT_ID;
	
    @Override
    protected void onCreate(Bundle savedInstanceState) {
    	requestWindowFeature(Window.FEATURE_NO_TITLE);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        iniciarAnuncio();

        final TextView numPreguntas = (TextView) findViewById(R.id.num_Preguntas);
        
        mSeekbar = (SeekBar) findViewById(R.id.seekBar1);
        mSeekbar.setOnSeekBarChangeListener(new OnSeekBarChangeListener(){
           public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser){
                numPreguntas.setText(Integer.toString(progress + 1));
           }
          public void onStartTrackingTouch(SeekBar seekBar) {}
          public void onStopTrackingTouch(SeekBar seekBar) {}
        });
        
        TestApplication.preguntas = new ArrayList<Pregunta>();
        
        bd = new AccesoBD(this);
        
    }

    public void startTest(View view){
    	String nums = "";
    	HashSet<Integer> aleatorios = new HashSet<Integer>();
    	aleatorios.clear();
    	Utils.resetTestApplication();
    	if(mSeekbar.getProgress()+1>TestApplication.PREGUNTAS_MAX){
    		TestApplication.preguntas_seleccionadas = TestApplication.PREGUNTAS_MAX;
    	}else{
    		TestApplication.preguntas_seleccionadas = mSeekbar.getProgress()+1;
    	}
    	for (int i=0;i<mSeekbar.getProgress()+1;i++){
    		  Integer al = new Integer(new java.util.Random().nextInt(TestApplication.PREGUNTAS_MAX));
    		  if(!aleatorios.add(al)){
    			  i--;
    			  continue;
    		  }
    		  nums = nums+", "+(al+1);
    		  Pregunta pregunta = Utils.getPreguntas(al, bd);
    		  TestApplication.preguntas.add(pregunta);
    	}
		  System.out.println(nums);
    	Intent intent = new Intent(this, TestActivity.class);
    	startActivity(intent);
    }
	// -----------------------------------------------------------------
	// METODOS PUBLICIDAD
	// -----------------------------------------------------------------

	public void iniciarAnuncio() {
		// Obtener el id de los anuncios
		UNIT_ID = getString(R.string.ad_banner_test);

		// Create the adView.
//		adView = new AdView(QuestionActivity.this, AdSize.BANNER, UNIT_ID);
		adView = new AdView(MainActivity.this);
		 adView.setAdUnitId(UNIT_ID);
		 adView.setAdSize(AdSize.BANNER);

		// Add the AdView to the view hierarchy. The view will have no size
		// until the ad is loaded.
		LinearLayout layout = (LinearLayout) findViewById(R.id.linearLayoutAnuncio);
		layout.addView(adView);

		// Create an ad request. Check logcat output for the hashed device ID to
		// get test ads on a physical device.
		 AdRequest adRequest = new AdRequest.Builder()
//		 .addTestDevice(AdRequest.DEVICE_ID_EMULATOR)
		 // .addTestDevice("INSERT_YOUR_HASHED_DEVICE_ID_HERE")
		 .build();

		// Start loading the ad in the background.
		 adView.loadAd(adRequest);
//		adView.loadAd(new AdRequest());
	}
    
}
